$(document).ready(function (){
    //请求用户信息，未登录的话显示登录与注册按钮
    $.ajax({
        url:"/user",
        type:"GET",
    }).done(function (resp){
        var username = resp.data.username
        // $("#auth").hide()
        var userinfo = `<div class="dropdown">
                    <button class="btn btn-secondary dropdown-toggle " type="button" id="userinfo_username" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Dropdown button
                    </button>
                    <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                        <a class="dropdown-item" href="#">Action</a>
                        <a class="dropdown-item" href="#">Another action</a>
                        <button class="dropdown-item" id="logout">logout</button>
                    </div>
                </div>`
        $("nav").append(userinfo)
        $("#userinfo_username").text(username)

        $("#logout").click(function (){
            $.ajax({
                url: "/account/logout",
                type: "GET",
            }).done(function (){
                location.reload()
            })
        })
    }).fail(function (data){
        $("#auth").removeAttr("hidden")
    })
})